//
//  UIImageView+YC.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/26.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "UIImageView+YC.h"
#import <FXBlurView/FXBlurView.h>

@implementation UIImageView (YC)


- (void)yc_setWebImageWithURLString:(NSString *)url
{
    NSURL *u;
    if((NSNull *)url != [NSNull null] && url.length > 0)
    {
        u = [NSURL URLWithString:url];
    }
    UIImage *holderImage = [UIImage yc_placeHolderImageForSize:self.bounds.size];
    [self sd_setImageWithURL:u placeholderImage:holderImage];
}

- (void)yc_setWebImageWithURLString:(NSString *)url holderSize:(CGSize)size;
{
    NSURL *u;
    if((NSNull *)url != [NSNull null] && url.length > 0)
    {
        u = [NSURL URLWithString:url];
    }
    UIImage *holderImage = [UIImage yc_placeHolderImageForSize:size];
    [self sd_setImageWithURL:u placeholderImage:holderImage];
}

- (void)yc_setAvatorWithURLString:(NSString *)url forSize:(CGSize)size
{
    [self yc_setAvatorWithURLString:url forSize:size holderImage:nil];
}


- (void)yc_setAvatorWithURLString:(NSString *)url forSize:(CGSize)size holderImage:(UIImage *)holderImage
{
    if(!holderImage)
    {
        self.image = [UIImage yc_roundPlaceHolderImageForSize:size];
    }
    else
    {
        self.image = holderImage;
    }
    
    NSURL *newURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://app.ios.gmall88.com/%@.png",[[url stringByAppendingString:NSStringFromCGSize(size)] yc_md5String]]];
    NSURL *oldURL = [NSURL URLWithString:url];
    
    YCWeakSelf;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager cachedImageExistsForURL:newURL completion:^(BOOL isInCache) {
        if(isInCache)
        {
            [self sd_cancelCurrentImageLoad];
            id <SDWebImageOperation> operation = [manager downloadImageWithURL:newURL
                                                                       options:0
                                                                      progress:nil
                                                                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                                         if(!weakSelf) return ;
                                                                         dispatch_async(dispatch_get_main_queue(), ^{
                                                                             weakSelf.image = image;
                                                                             [weakSelf setNeedsLayout];
                                                                         });
                                                                     }];
            [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
        }
        else
        {
            [self sd_cancelCurrentImageLoad];
            id <SDWebImageOperation> operation = [manager downloadImageWithURL:oldURL
                                                                       options:0
                                                                      progress:nil
                                                                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                                         if(image && !error)
                                                                         {
                                                                             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                                                                 UIImage *newImage = [image yc_roundImageWithSize:size radius:size.width / 2.0];
                                                                                 NSString *key = [manager cacheKeyForURL:newURL];
                                                                                 //[self.imageCache storeImage:image forKey:key toDisk:YES];
                                                                                 
                                                                                 [manager.imageCache storeImage:newImage recalculateFromImage:YES imageData:UIImagePNGRepresentation(newImage) forKey:key toDisk:YES];
                                                                                 
                                                                                 if(!weakSelf) return ;
                                                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                                                     weakSelf.image = newImage;
                                                                                     [weakSelf setNeedsLayout];
                                                                                 });
                                                                             });
                                                                         }
                                                                         
                                                                     }];
            [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
        }
    }];
}

- (void)yc_setBlurImageWithURLString:(NSString *)url holderSize:(CGSize)size
{
    
    UIImage *holderImage = [UIImage yc_placeHolderImageForSize:size];
    self.image = holderImage;
    
    NSURL *newURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://app.ios.gmall88.com/%@.png",[[url stringByAppendingString:NSStringFromCGSize(size)] yc_md5String]]];
    NSURL *oldURL = [NSURL URLWithString:url];
    
    YCWeakSelf;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager cachedImageExistsForURL:newURL completion:^(BOOL isInCache) {
        if(isInCache)
        {
            [self sd_cancelCurrentImageLoad];
            id <SDWebImageOperation> operation = [manager downloadImageWithURL:newURL
                                                                       options:0
                                                                      progress:nil
                                                                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                                         if(!weakSelf) return ;
                                                                         dispatch_async(dispatch_get_main_queue(), ^{
                                                                             weakSelf.image = image;
                                                                             [weakSelf setNeedsLayout];
                                                                         });
                                                                     }];
            [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
        }
        else
        {
            [self sd_cancelCurrentImageLoad];
            id <SDWebImageOperation> operation = [manager downloadImageWithURL:oldURL
                                                                       options:0
                                                                      progress:nil
                                                                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                                         if(image && !error)
                                                                         {
                                                                             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                                                                 UIImage *newImage = [image blurredImageWithRadius:14.0 iterations:3 tintColor:nil];
                                                                                 NSString *key = [manager cacheKeyForURL:newURL];
                                                                                 
                                                                                 [manager.imageCache storeImage:newImage recalculateFromImage:YES imageData:UIImagePNGRepresentation(newImage) forKey:key toDisk:YES];
                                                                                 
                                                                                 if(!weakSelf) return ;
                                                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                                                     weakSelf.image = newImage;
                                                                                     [weakSelf setNeedsLayout];
                                                                                 });
                                                                             });
                                                                         }
                                                                         
                                                                     }];
            [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
        }
    }];
}







@end
